package com.cfr.utils;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;

public class StringUtils {
	private static Logger logger = Logger.getLogger(StringUtils.class);

	public static boolean isNullString(String content) {
		return content == null || "".equals(content);

	}

	public static String parseDateFormat(String date) {
		String returnValue = "";
		try {
			SimpleDateFormat dateformat = new SimpleDateFormat(
					"E MMM dd HH:mm:ss z yyyy");
			Date nowDate = dateformat.parse(date);
			SimpleDateFormat dateformat1 = new SimpleDateFormat(
					"MM/dd/yyyy HH:mm:ss");
			returnValue = dateformat1.format(nowDate);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return returnValue;
	}

	public static boolean isNULL(String content) {
		return "NULL".equals(content);
	}

	public static String getSetFunction(String arg) {
		StringBuffer sb = new StringBuffer();
		sb.append("set");
		sb.append(arg.substring(0, 1).toUpperCase());
		sb.append(arg.substring(1, arg.length()));
		return sb.toString();
	}

	public static int average(int x, int y) {
		logger.info(x & y);
		logger.info((x ^ y) >> 1);
		return (x & y) + ((x ^ y) >> 1);
	}

	public static String docToString(Document doc) {

		String xmlStr = "";
		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			javax.xml.transform.Transformer t = tf.newTransformer();
			// t.setOutputProperty("encoding", "UTF-8");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			t.transform(new DOMSource(doc), new StreamResult(bos));
			xmlStr = bos.toString();
		} catch (TransformerConfigurationException e) {
			logger.error(e.getMessage(), e);
		} catch (TransformerException e) {
			logger.error(e.getMessage(), e);
		}
		return xmlStr;
	}

	public static Boolean isEmpty(String string) {
		if (string != null && !"".equalsIgnoreCase(string)) {
			return false;
		} else {
			return true;
		}

	}

}
